
#include <QWidget>
#include <QFile>
#include <QImage>
#include "QColorRampEditor.h"
#include <iostream>
using namespace std;

class DrawIm : public QWidget
{
	Q_OBJECT
public:
	/// Constructor
	DrawIm(QWidget* parent=0) : QWidget(parent)
    {
        if (QFile::exists("lena.jpg"))
        {
            qimg_ = QImage("lena.jpg");
            qimg_ = qimg_.convertToFormat(QImage::Format_Indexed8, Qt::MonoOnly);
            setMinimumSize(qimg_.width(),qimg_.height());
        }
        else
            cout << "Image not found" << endl;
    }

	/// update the color map of the image from colorRamp
	void updateImage(QColorRampEditor* colorramp)
	{
        //QVector<QPair<qreal, QColor> > ramp = colorramp->getRamp();
		QVector<QRgb> ctable = colorramp->getColorTable();		
		qimg_.setColorTable(ctable);
		update();
	}

public slots:

	/// react on signal
	void onRampChanged()
	{
		updateImage(reinterpret_cast<QColorRampEditor*>(sender()));
	}
protected:

	/// repaint teh widget
	void paintEvent(QPaintEvent *)
    {
		QPainter p(this);
        p.fillRect(rect(), QColor(0, 0, 0));
		p.drawImage(0,0,qimg_);
    }

private:
	/// the image
	QImage qimg_;
};
